<template>
  <!-- 
        action: 请求 URL (自动生成需要有值)
        auto-upload：是否自动上传文件
        show-file-list：是否显示已上传文件列表
        on-preview：点击文件列表中已上传的文件时的钩子（预览）
        limit：允许上传文件的最大数量
        before-upload：上传文件之前的钩子，参数为上传的文件， 若返回false或者返回 Promise 且被 reject，则停止上传。
    -->
  <!-- :on-preview="handlePreviewAvatar" :on-remove="handleAvatarRemove" -->
  <el-upload
    ref="uploadAvatar"
    class="avatar-uploader"
    :action="''"
    :auto-upload="false"
    :show-file-list="false"
    :limit="1"
    :before-upload="beforeAvatarUpload"
    :on-change="handleAvatarChange"
    :style="{ '--width': width, '--height': height }"
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
    <div class="avatar-delete" v-if="imageUrl" @click.stop="handleAvatarRemove">
      <el-icon color="#409EFC"><Delete /></el-icon>
    </div>
  </el-upload>
</template>
<script setup>
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus, Delete } from '@element-plus/icons-vue'

// 父组件传参
const props = defineProps({
  width: {
    type: String,
    default: '100px'
  },
  height: {
    type: String,
    default: '100px'
  },
  avatarUrl: {
    type: String,
    default: ''
  }
})

const emits = defineEmits(['onChange']) // 父组件调用
const imageUrl = ref('')

// 文件上传前检查
const beforeAvatarUpload = (rawFile) => {
  if (
    !['image/jpeg', 'image/png', 'image/jpg', 'image/gif'].indexOf(rawFile.type)
  ) {
    // ElMessage.error('Avatar picture must be image type!')
    ElMessage.error('头像图片必须为image类型!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 10) {
    // ElMessage.error('Avatar picture size can not exceed 2MB!')
    ElMessage.error('头像图片大小不能超过10MB！')
    return false
  }
  return true
}
// 添加文件后处理事件
const handleAvatarChange = (file, fileList) => {
  imageUrl.value = URL.createObjectURL(file.raw)
  emits('onChange', file.raw)
}
// 删除头像
const uploadAvatar = ref(null)
const handleAvatarRemove = () => {
  imageUrl.value = ''
  uploadAvatar.value.clearFiles()
  emits('onRemove')
}

// 监听地址变化
watch(
  () => props.avatarUrl,
  () => {
    props.avatarUrl && (imageUrl.value = props.avatarUrl)
  },
  { deep: true, immediate: true }
)
</script>
<style scoped>
.avatar-uploader .avatar {
  width: var(--width);
  height: var(--height);
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: var(--width);
  height: var(--height);
  text-align: center;
}

.avatar-delete {
  /* position: absolute;
  right: 0;
  top: 0;
  padding: 4px 6px;
  background: rgba(0, 0, 0, 0.2);
  border-bottom-left-radius: 6px;
  border-top-right-radius: 6px; */
  position: absolute;
  right: 1px;
  top: 0;
  width: 25px;
  height: 25px;
  line-height: 25px;
  text-align: center;
  background: rgba(0, 0, 0, 0.2);
  border-bottom-left-radius: 6px;
}
</style>
